package greendao.example;


import greendao.example.DaoMaster;
import greendao.example.DaoSession;
import greendao.example.Room;
import greendao.example.RoomDao;
import greendao.example.RoomDao.Properties;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import de.greenrobot.dao.query.WhereCondition.StringCondition;
import de.greenrobot.daoexample.R;


public class MyActivity extends Activity{
	
	
	private DaoMaster daoMaster;
    private DaoSession daoSession;
    
    private RoomDao roomDao;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.demo);
        
        MyApplication myApplication =  MyApplication.getInstance();  
        daoMaster = myApplication.getDaoMaster(this);
        daoSession = myApplication.getDaoSession(this);
        
        roomDao = daoSession.getRoomDao();  
        
        addRoom();
        
	}     

	
	private void addRoom() {
        
		System.out.print("add room......\n");
		
		roomDao.queryBuilder().LOG_SQL=true;
		roomDao.queryBuilder().LOG_VALUES=true;
		
		//read records
		List<Room> retList = roomDao.loadAll();
		
		//insert records
		if(retList.size()==0){
			
			Room room1 = new Room(11L,100,200,250,1000);
	        roomDao.insert(room1);
	        
	        Room room2 = new Room(130L,101,300,290,8000);
	        roomDao.insert(room2);
			
		}
		 
        
        for (Room room: retList){
        	
        	System.out.print("room id: "+ room.getId()+"\n");
        }
        
        
        //update records
        Room newRoom = new Room(11L,101,201,251,1001);
        roomDao.update(newRoom);
        
        
        //and query
        List<Room> andQueryList = roomDao.queryBuilder().where(Properties.Height.eq(8000), Properties.Width.eq(290)).list();
        
        
        //or query
        List<Room> orQueryList = roomDao.queryBuilder().where(roomDao.queryBuilder().or(Properties.Height.eq(8000), Properties.Price.eq(101))).list();
		
        
        
        //raw sql
        
        try{
//        	List<Room> queryList = roomDao.queryBuilder().where(new StringCondition("select * from  ROOM")).build().list();
        	List<Room> rawQueryList = roomDao.queryBuilder().where(new StringCondition("Height=8000 ")).build().list();
        	
        	 for (Room room : rawQueryList) {

     			System.out.print("query  room id: " + room.getId() + "\n");
     		}

        }catch(Exception e){
        	
        }
        
        
       		
		
		//complex query
		
		
		
        
	}    
	
	
	
}
